<template>
	<view scroll-with-animation scroll-y style="height: 100%;position: relative;background-color: #F6FAFA;">
		<view class="list pl25 pr25" style="text-align: center;background-color: #F6FAFA;padding-top: 10rpx;" v-if="recipieList&&recipieList.length>0">
			<view class="chufang-card" v-for=" item in recipieList" :key="item.recipeId" @click="tochufangxq(item)">
				<view class="chufang-card-row">
					<text style="color: #00BAAD;margin-right: 10rpx;">【诊断】</text>
					<text v-for="(diseaItem,diseaIdx) in item.diseaInfo" :key="diseaIdx">{{diseaItem.name}}</text>
				</view>
				<view class="chufang-card-row">
					<text style="margin-right: 10rpx;">处方号：</text>
					<text>{{item.recipeCode}}</text>
				</view>
				<view class="chufang-card-row">
					<text style="margin-right: 10rpx;">医生：</text>
					<text>{{item.doctorName}}</text>
				</view>
				<view class="chufang-card-row">
					<text style="margin-right: 10rpx;">RP:</text>
					<text v-for="(recipeItem,recipeIdx) in item.doctorNormalRecipeDrug" :key="recipeIdx">{{recipeItem.drugName+recipeItem.drugSpecificationName}}</text>
				</view>
				<view class="chufang-card-row">
					<text style="margin-right: 10rpx;">时间：</text>
					<text>{{formatDate(item.sendDate)}}</text>
				</view>
				<view class="chufang-btn">
					<view v-if="medDocInfoId == 0 " style="border: 2rpx solid #84DDD7;width: 150rpx;height: 50rpx;line-height:50rpx;border-radius: 25rpx;text-align: center;color: #00BAAD;" @click.stop="tochufangBuy(item)">复购</view>
					<view style="border: 2rpx solid #84DDD7;width: 150rpx;height: 50rpx;line-height:50rpx;border-radius: 25rpx;text-align: center;color: #00BAAD;" @click.stop="returnAdvisoryvideo(item)" v-else>选择</view>
				</view>
			</view>
			<uni-load-more :status="message" :contentText="contentText" @clickLoadMore="loadMore"></uni-load-more>
		</view>
		<view class="empty-list" v-else>
			<image style="height: 280rpx;" mode="heightFix" src="../../static/images/null.png"></image>
			<view style="color: #A7ADAD;margin-top: 20rpx;margin-bottom: 20rpx;">尚未查询到您的信息</view>
			<view class="btn" @click="toDoctorlist">找医生开处方</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				wx: "https://app.mzmzb.com",
				current: 1,
				hasNextPage: true, //是否有下一页
				message: 'more',
				allDocList: [],
				recipieList: [],
				contentText: {
					contentdown: "点击显示更多",
					contentrefresh: "正在加载...",
					contentnomore: "没有更多数据了"
				},
				medDocInfoId:0,
				usrPersonalId:0,
				isChinOrder:0,
				usrUserId:0
			};
		},
		onLoad(option) {
			if(option.medDocInfoId=='undefined'||option.medDocInfoId==undefined){
				this.medDocInfoId=0
			}else{
				this.medDocInfoId = option.medDocInfoId;
			}
			
			if(option.usrPersonalId=='undefined'||option.usrPersonalId==undefined){
				this.usrPersonalId=0
			}else{
				this.usrPersonalId = option.usrPersonalId;
			}
			
			if(option.isChinOrder=='undefined'||option.isChinOrder==undefined){
				this.isChinOrder=0
			}else{
				this.isChinOrder = option.isChinOrder;
			}
			this.usrUserId = uni.getStorageSync("usrUserId");
			this.getRecipieList();
		},
		onPullDownRefresh() {
			console.log('onPullDownRefresh==========')
		},
		onReachBottom(){
			if (this.message == 'more') {
				this.message = 'loading';
				this.current = this.current + 1;
				setTimeout(() => {
					this.getRecipieList()
					// if (this.hasNextPage) {
					// 	this.message = 'more';
					// } else {
					// 	this.message = 'noMore';
					// }
				}, 500);
			}
		},
		methods: {
			toDoctorlist(){
				uni.navigateTo({
					url: '/pages4/doctorlist/index'
				});
			},
			tochufangBuy(repRecipeInfo){
				let sendDate = repRecipeInfo.sendDate
				let date1 = new Date(sendDate);
				let date2 = new Date();
				// let m1 = date1.getMonth() + 1,
				// let m2 = date1.getMonth() + 1,
				let time1 = Date.parse(new Date(date1));
				let time2 = Date.parse(new Date(date2));
				let nDays = Math.abs(parseInt((time2 - time1) / 1000 / 3600 / 24 / 30));
				if( nDays > 6 ){
					uni.showToast({
						title: '6个月内的处方才可复购',
						icon: 'error'
					})
					return 
				}
				let param = 'medOfficinaInfoId='+repRecipeInfo.medOfficinaInfoId
				param += '&medOfficinaInfoName='+repRecipeInfo.medOfficinaInfoName
				param += '&doctorNormalRecipeDrug='+JSON.stringify(repRecipeInfo.doctorNormalRecipeDrug)
				param += '&recipeId='+repRecipeInfo.recipeId
				param += '&usrUserId='+this.usrUserId
				uni.navigateTo({
				    url: '/pages3/yaofang/index?'+param
				});
			},
		    tochufangxq(repRecipeInfo){
				// let param = 'medOfficinaInfoId='+repRecipeInfo.medOfficinaInfoId
				// param += '&recipeId='+repRecipeInfo.recipeId
				// param += '&usrUserId='+this.usrUserId
				// uni.navigateTo({
				//     url: '/pages3/chufangxq/index?'+param
				// });
				
				uni.navigateTo({
				    url: '/pages3/prescriptiondetail/index?recipeId='+repRecipeInfo.recipeId
				});
			},
			returnAdvisoryvideo(repRecipeInfo) {
				let pages = getCurrentPages();
				let prevPage = pages[pages.length-2]
				prevPage.$vm.chufangRecords = repRecipeInfo.recipeId
				uni.navigateBack({
					delta:1
				})
			},
		
			getRecipieList() {
				var that = this
				let openid=uni.getStorageSync('openId')
				let data = {
					//usrUserId: 5557,
					usrUserId: this.usrUserId,
					current: this.current,
					hosId: this.hspId
				}
				if(this.usrPersonalId !=0 ){
					data.usrPersonalId = this.usrPersonalId
					data.isChinOrder = this.isChinOrder
				}
				this.func.$request('repRecipe/getRecipieList.tz',data,GET).then(res=>{
					if (res.data.success = true) {
						let list = JSON.parse(JSON.stringify(res.data.result))
						list = list.filter(item=>item.payState== 2)
						if(list.length > 0){
							if(this.recipieList.length>0){
								this.recipieList.push(...list)
							}else{
								this.recipieList = list
							}
							if(list.length < 10){
								this.message = 'noMore';
							}else{
								this.message = 'more';
							}
						}else{
							this.message = 'noMore';
						}
					} else {
						this.message = 'noMore';
					}
				})
			},
			//加载更多
			loadMore(e) {
				if (e.detail.status == 'more') {
					this.message = 'loading';
					this.current = this.current + 1;
					setTimeout(() => {
						this.getRecipieList()
						// if (this.hasNextPage) {
						// 	this.message = 'more';
						// } else {
						// 	this.message = 'noMore';
						// }
					}, 500);
				}
			},
			formatDate(value) {
			  var date = new Date(value);
			  var y = date.getFullYear(),
			    m = date.getMonth() + 1,
			    d = date.getDate(),
			    h = date.getHours(),
			    i = date.getMinutes(),
			    s = date.getSeconds();
			  if (m < 10) { m = '0' + m; }
			  if (d < 10) { d = '0' + d; }
			  if (h < 10) { h = '0' + h; }
			  if (i < 10) { i = '0' + i; }
			  if (s < 10) { s = '0' + s; }
			  var t = y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s;
			  return t;
			}
		}
	};
</script>

<style lang="less">
	.list{
		.chufang-card{
			border-top: 1rpx solid #efefef;
			border: 1px solid #F2F9F9;
			box-shadow: 0px 1px 17px 1px rgba(162, 215, 211, 0.16);
			border-radius: 32rpx;
			background-color: #FFFFFF;
			margin-top: 20rpx;
			padding-left: 40rpx;
			padding-top: 20rpx;
			.chufang-card-row{
				width: calc( 100% - 40rpx );
				text-align: left;
				margin-bottom: 20rpx;
				word-break:break-all;
			}
			.chufang-btn{
				width: calc(100% - 40rpx);
				border-top: 5rpx solid #E6EAEA;
				padding-top: 20rpx;
				padding-bottom: 20rpx;
				margin-top: 20rpx;
				display: flex;
				justify-content: flex-end;
			}
		}
	}
	.empty-list{
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		width: 100vw;
		height: 100vh;
		background-color: #FFFFFF;
		.btn{
			width: 320rpx;
			height: 88rpx;
			line-height: 88rpx;
			text-align: center;
			color: #FFFFFF;
			background: #BE965B;
			border-radius: 44rpx;
		}
	}
</style>
	